package graphics;

import java.awt.Canvas;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.media.j3d.Raster;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.GraphicsContext3D;
import javax.media.j3d.ImageComponent;
import javax.media.j3d.ImageComponent2D;
import javax.vecmath.Point3f;

/**
 *
 * @author George
 */
public class ImageUtils {
    
    public ImageUtils() {
        
    }
    
    public static BufferedImage canvas3DToImage(Canvas3D canvas) {
        GraphicsContext3D ctx = canvas.getGraphicsContext3D();
        int w = canvas.getWidth();
        int h = canvas.getHeight();
        BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB );
        ImageComponent2D im = new ImageComponent2D(ImageComponent.FORMAT_RGB, bi );        
        Raster ras = new Raster(new Point3f( -1.0f, -1.0f, -1.0f ), Raster.RASTER_COLOR, 0, 0, w, h, im, null);
        ctx.readRaster(ras);
        return ras.getImage().getImage();
    }
    
    
    //Trecho de código disponível em http://www.bytemycode.com/snippets/snippet/689/
    //Acesso em: 01/06/2009
    public static BufferedImage canvasToImage(Canvas cnvs) {
        int w = cnvs.getWidth();
        int h = cnvs.getHeight();
        int type = BufferedImage.TYPE_INT_RGB;
        BufferedImage image = new BufferedImage(w,h,type);
        Graphics2D g2 = image.createGraphics();
        cnvs.paint(g2);
        g2.dispose();
        return image;
    }
    
}
